package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.AdminGoodsDeleteShoppingInformationBO;
import com.abaaba.project2.bean.vo.*;
import com.abaaba.project2.mapper.*;
import com.abaaba.project2.service.AdminGoodsService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.PreparedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public AdminGoodsDataSendingListVO goodsSearchAll(GoodsDataSendingListBaseReq baseReq) {
        PageHelper.startPage(baseReq.getPage(),baseReq.getLimit());
        AdminGoodsDataSendingListVO goodsDataSendingListVO = new AdminGoodsDataSendingListVO();
        GoodsExample goodsExample = new GoodsExample();
        if (baseReq.getGoodsSn()!=null){
        //Integer goodsSn = Integer.parseInt(baseReq.getGoodsSn());
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        /*.andNameEqualTo("%"+baseReq.getName()+"%")*/
        criteria.andIdEqualTo(baseReq.getGoodsSn());
        }/*else if (baseReq.getGoodsSn()==null){
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andNameEqualTo(baseReq.getName());
        }*/
        if (baseReq.getName()!=null){
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            //criteria.andNameEqualTo(baseReq.getName());
            criteria.andNameLike("%"+baseReq.getName()+"%");
        }
        if (baseReq.getName()!=null&&baseReq.getGoodsSn()!=null){
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andNameLike("%"+baseReq.getName()+"%").andIdEqualTo(baseReq.getGoodsSn());;
        }
        AdminGoodsDataSendingListVO.DataBean dateBean = new AdminGoodsDataSendingListVO.DataBean();
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
       // List<Goods> goods = goodsMapper.selectByGoods();


        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        long total = goodsPageInfo.getTotal();
        /*goodsPageInfo这个就是当前查询分页信息*/
        dateBean.setTotal((int)total);//这是查询总数据库

        /*  private int id;private String goodsSn;private String name;
            private int categoryId; private int brandId; private String keywords; private String brief;
            private boolean isOnSale; private int sortOrder; private String picUrl; private boolean isNew;
            private boolean isHot; private String unit; private int counterPrice; private int retailPrice;
            private String addTime; private String updateTime; private boolean deleted; private String detail;
            private String shareUrl;private List<String> gallery;*/
        List<AdminGoodsDataSendingListVO.DataBean.ItemsBean> itemsBeans = new ArrayList<>();
        for (Goods good : goods) {
            //String[] strings = stringToInteger(good.getGallery());

            AdminGoodsDataSendingListVO.DataBean.ItemsBean itemsBean = new AdminGoodsDataSendingListVO.DataBean.ItemsBean(good.getId(),good.getGoodsSn(),good.getName(), good.getCategoryId(),good.getBrandId(),good.getKeywords(),good.getBrief(),good.getIsOnSale(),good.getSortOrder(),good.getPicUrl(),good.getIsNew(), good.getIsHot(), good.getUnit(), good.getCounterPrice(), good.getRetailPrice(), good.getAddTime(), good.getUpdateTime(), good.getDeleted(), good.getDetail(), good.getShareUrl(), good.getGallery());
            itemsBeans.add(itemsBean);
        }

        dateBean.setItems(itemsBeans);//这是放查询所有数据

        goodsDataSendingListVO.setErrno(0);
        goodsDataSendingListVO.setData(dateBean);
        goodsDataSendingListVO.setErrmsg("成功");
        return goodsDataSendingListVO;
    }

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public AdminGoodsAddShoppingInformationOfCatAndBrandVO adminGoodsAddShoppingInformation() {
        CategoryExample categoryExample = new CategoryExample();
        AdminGoodsAddShoppingInformationOfCatAndBrandVO adminGoodsAddShoppingInformationOfCatAndBrand = new AdminGoodsAddShoppingInformationOfCatAndBrandVO();

        List<AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean> categoryListBean = new ArrayList<>();
        //先查询这个表
        Category category = new Category();
        //List<Category> listCategory = categoryMapper.selectByExample(categoryExample);
        List<Category> listCategory = categoryMapper.selectAllByCategory();
        for (Category category1 : listCategory) {
            Integer integer1 = null;
            /*这里就是说查到1，然后把数据传入*/
            List<AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean.ChildrenBean> childrenBeanList = null;
            AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean categoryListBean1 = null;

            /*这里错了，错误原因是只有等于l1才能进去*/
            if ("L1".equals(category1.getLevel())){
            integer1 = category1.getId();/*等于li就把数据取出来，这就是获得他的id,再根据ID查pid*/
            /*当等于l1的时候进来，再根据*/
            /*现在就是把所有pid为100501的数据，将他的ID和名字传入*/
            childrenBeanList = new ArrayList<>();

                /*根据l1的id找数据，重新封装到一个list*/
                List<Category> categoryList = categoryMapper.selectByCategoryPid(category1.getId());
                for (Category category2 : categoryList) {
                    if (integer1.equals(category2.getPid())){/*然后看l1的数据是否相等*/
                    AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean.ChildrenBean categoryGetPidBO = new AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean.ChildrenBean(category2.getId(),category2.getName());
                    childrenBeanList.add(categoryGetPidBO);
                }
            }
                categoryListBean1 = new AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean(category1.getId(),category1.getName(),childrenBeanList);
            }
            if (categoryListBean1!=null){
                categoryListBean.add(categoryListBean1);
            }
            //AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean.ChildrenBean childrenBean1 = new AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.CategoryListBean.ChildrenBean();
         }

        BrandExample brandExample = new BrandExample();
        /*同样的先查询类再返回数据*/
        //List<Brand> listBrand = brandMapper.selectByExample(brandExample);
        List<Brand> listBrand = brandMapper.selectByBrandAll();
        List<AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.BrandListBean> brandListBean = new ArrayList<>();
        for (Brand brand : listBrand) {
            AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.BrandListBean brandListBean1 = new AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean.BrandListBean(brand.getId(),brand.getName());
            brandListBean.add(brandListBean1);
        }

        AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean dataBean = new AdminGoodsAddShoppingInformationOfCatAndBrandVO.DataBean(categoryListBean,brandListBean);
        adminGoodsAddShoppingInformationOfCatAndBrand.setData(dataBean);
        adminGoodsAddShoppingInformationOfCatAndBrand.setErrmsg("成功");
        adminGoodsAddShoppingInformationOfCatAndBrand.setErrno(0);

        return adminGoodsAddShoppingInformationOfCatAndBrand;
    }

    @Override
    public synchronized int adminGoodsDeleteShoppingInformation(AdminGoodsDeleteShoppingInformationBO adminGoodsDeleteShoppingInformationBO) {
        /*GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIdEqualTo(adminGoodsDeleteShoppingInformationBO.getId());*/
        //int i = goodsMapper.deleteByExample(goodsExample);/*这把所有的都删除了，傻逼*/
        Integer id = adminGoodsDeleteShoppingInformationBO.getId();

        int i = goodsMapper.deleteByGoodsInformation(id);
        //categoryMapper   goodsAttributeMapper goodsProductMapper goodsSpecificationMapper

        //int codeCategory = categoryMapper.updateByPrimaryAndDeleted(id,deleted);
        int codeAttribute = goodsAttributeMapper.deleteByPrimaryAndDeleted(id);
        int codeProduct = goodsProductMapper.deleteByPrimaryAndDeleted(id);
        int codeSpecification = goodsSpecificationMapper.deleteByPrimaryAndDeleted(id);

        if (i == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public synchronized int  adminGoodsCreatShopping(AdminGoodsAddShoppingInformationOfGoosListVO agas) {

        java.util.Date NowTime = new java.util.Date();
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentTime=new Date(java.lang.System.currentTimeMillis());
        String dateString=formatter.format(currentTime);
        String dateString1 = dateString;
        agas.getGoods();

        List<AdminGoodsAddShoppingInformationOfGoosListVO.SpecificationsBean> specifications = agas.getSpecifications();
        List<AdminGoodsAddShoppingInformationOfGoosListVO.ProductsBean> products = agas.getProducts();
        List<AdminGoodsAddShoppingInformationOfGoosListVO.AttributesBean> attributes = agas.getAttributes();
        /*private GoodsBean goods;
    private List<SpecificationsBean> specifications;
    private List<ProductsBean> products;
    private List<AttributesBean> attributes;*/
        Short shortNumber = 0;
        Goods goods = new Goods(null,
                agas.getGoods().getGoodsSn(),
                agas.getGoods().getName(),
                agas.getGoods().getCategoryId(),
                agas.getGoods().getBrandId(),
                agas.getGoods().getGallery(),
                agas.getGoods().getKeywords(),
                agas.getGoods().getBrief(),
                agas.getGoods().isIsOnSale(),
                shortNumber,
                agas.getGoods().getPicUrl(),
                null,
                agas.getGoods().isIsNew(),
                agas.getGoods().isIsHot(),
                agas.getGoods().getUnit(),
                agas.getGoods().getCounterPrice(),
                agas.getGoods().getRetailPrice(),currentTime,null,false,null);


        //GoodsAttribute goodsAttribute = new GoodsAttribute(null,null,agas.getAttributes().getAttribute(),agas.getAttributes().getValue(),null,null,null);
        //GoodsProduct goodsProduct = new GoodsProduct(null,null,);
        /*public GoodsSpecification(Integer id, Integer goodsId, String specification, String value, String picUrl, Date addTime, Date updateTime, Boolean deleted) {
         */
      //  GoodsSpecification goodsSpecification = new GoodsSpecification(null,null,agas.getSpecifications().getSpecification(),agas.getSpecifications().getValue(),agas.getSpecifications().getPicUrl(),null,null,null);

        //int code1 = goodsMapper.insertIntoGoods(goods);
        int insert = goodsMapper.insert(goods);
        java.lang.System.out.println(dateString);

        int goods1 = goodsMapper.selectById();
        java.lang.System.out.println(goods1);
        /*现在要获取他的ID*/
        int code2 =0;
        for (AdminGoodsAddShoppingInformationOfGoosListVO.SpecificationsBean specification : specifications) {
            GoodsSpecification goodsSpecification = new GoodsSpecification(null,goods1,specification.getSpecification(),specification.getValue(),specification.getPicUrl(),currentTime,null,false);
            code2 =goodsSpecificationMapper.insert(goodsSpecification);
        }
        int code3 =0;
        for (AdminGoodsAddShoppingInformationOfGoosListVO.ProductsBean product : products) {
            List<String> list = product.getSpecifications();  /*这个Specifications存一个list*/
            String[] strings = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                strings[i] = list.get(i);
            }
            GoodsProduct goodsProduct = new GoodsProduct(null,goods1,strings,product.getPrice(),product.getNumber(),product.getUrl(),currentTime,null,false);
            code3 = goodsProductMapper.insert(goodsProduct);
        }

        int code4 =0;
        for (AdminGoodsAddShoppingInformationOfGoosListVO.AttributesBean attribute : attributes) {
            GoodsAttribute goodsAttribute = new GoodsAttribute(null,goods1,attribute.getAttribute(),attribute.getValue(),currentTime,null,false);
            code4 =  goodsAttributeMapper.insert(goodsAttribute);
        }

        //goodsAttributeMapper.insert(goodsAttribute);
       // int code2 = goodsAttributeMapper.insertIntoAttribute(goodsAttribute);
        //int code3 =  goodsProductMapper.insertIntoProduct(goodsProduct);
       // int code4 = goodsSpecificationMapper.insertIntoSpecification(goodsSpecification);

        if (insert == 1 && code2 ==1 && code3 ==1){
            /*失败原因code3等于0*/
            return 200;
        }
        return 404;
    }

    @Override
    public synchronized AdminGoodsAddShoppingInformationOfGoosListTwoVO adminGoodsUpdateShoppingInformation(Integer id) {

        /*先取good数据*/
        Goods goods1 = goodsMapper.selectByPrimaryKey(id);

        /*然后取出Specification数据*/
        GoodsSpecification specifications1 = goodsSpecificationMapper.selectByPrimaryId(id);

        /*然后取出Product*/
        GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryId(id);

        /*取出Attribute数据*/
        //GoodsAttribute goodsAttribute = goodsAttributeMapper.selectByPrimary(id);

       AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.GoodsBean goods = new AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.GoodsBean(goods1.getId(),goods1.getGoodsSn(),goods1.getName(),goods1.getCategoryId(),goods1.getBrandId(),goods1.getKeywords(),goods1.getBrief(),goods1.getIsOnSale(),goods1.getSortOrder(),goods1.getPicUrl(),goods1.getShareUrl(),goods1.getIsNew(),goods1.getIsHot(),goods1.getUnit(),goods1.getCounterPrice(),goods1.getRetailPrice(),goods1.getAddTime(),goods1.getUpdateTime(),goods1.getDeleted(),goods1.getDetail(),goods1.getGallery());

        List<AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.SpecificationsBean> specifications = new ArrayList<>();
        AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.SpecificationsBean specificationsBean = new AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.SpecificationsBean(specifications1.getId(),specifications1.getGoodsId(),specifications1.getSpecification(),specifications1.getValue(),specifications1.getPicUrl(),specifications1.getAddTime(),specifications1.getUpdateTime(),specifications1.getDeleted());
        specifications.add(specificationsBean);

        List<AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.ProductsBean> products = new ArrayList<>();
        AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.ProductsBean productsBean = new AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.ProductsBean(goodsProduct.getId(),goodsProduct.getGoodsId(),goodsProduct.getPrice(),goodsProduct.getNumber(),goodsProduct.getUrl(),goodsProduct.getAddTime(),goodsProduct.getUpdateTime(),goodsProduct.getDeleted(),goodsProduct.getSpecifications());
        products.add(productsBean);

        List<AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.AttributesBean> attributes = new ArrayList<>();
        AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.AttributesBean attributesBean = new AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean.AttributesBean();
        attributes.add(attributesBean);


        //分类
        Category category = categoryMapper.selectByPrimaryKey(goods.getCategoryId());
        Integer[] categoryIds = new Integer[2];
        categoryIds[0] = category.getPid();
        categoryIds[1] = goods.getCategoryId();//1008009

        AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean dataBean = new AdminGoodsAddShoppingInformationOfGoosListTwoVO.DataBean(goods,categoryIds,attributes,specifications,products);
        /*从四个表取出数据然后放在一个表里*/
        AdminGoodsAddShoppingInformationOfGoosListTwoVO adgas = new AdminGoodsAddShoppingInformationOfGoosListTwoVO(0,dataBean,"成功");
        return adgas;
    }

    @Override
    public synchronized int adminGoodsUpdateShoppingInformationOfGoodsListVO(AdminGoodsUpdateShoppingInformationOfGoodsListVO adgs) {

        java.util.Date NowTime = new java.util.Date();
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentTime=new Date(java.lang.System.currentTimeMillis());
        String dateString=formatter.format(currentTime);


        adgs.getAttributes();
        adgs.getGoods();
        adgs.getProducts();
        adgs.getSpecifications();


        List<AdminGoodsUpdateShoppingInformationOfGoodsListVO.SpecificationsBean> specifications = adgs.getSpecifications();
        List<AdminGoodsUpdateShoppingInformationOfGoodsListVO.ProductsBean> products =  adgs.getProducts();
        List<AdminGoodsUpdateShoppingInformationOfGoodsListVO.AttributesBean> attributes = adgs.getAttributes();

        Goods goods = new Goods(adgs.getGoods().getId(),
                adgs.getGoods().getGoodsSn(),
                adgs.getGoods().getName(),
                adgs.getGoods().getCategoryId(),
                adgs.getGoods().getBrandId(),
                adgs.getGoods().getGallery(),
                adgs.getGoods().getKeywords(),
                adgs.getGoods().getBrief(),
                adgs.getGoods().isOnSale(),
                adgs.getGoods().getSortOrder(),
                adgs.getGoods().getPicUrl(),
                adgs.getGoods().getShareUrl(),
                adgs.getGoods().isNew(),
                adgs.getGoods().isHot(),
                adgs.getGoods().getUnit(),
                adgs.getGoods().getCounterPrice(),
                adgs.getGoods().getRetailPrice(),null,currentTime,false,adgs.getGoods().getDetail());
        /*现在有他的ID*/
        /*现在要获取他的ID*/

        int insert = goodsMapper.updateByPrimaryKey(goods);

        int code2 =0;
        for (AdminGoodsUpdateShoppingInformationOfGoodsListVO.SpecificationsBean specification : specifications) {
            GoodsSpecification goodsSpecification = new GoodsSpecification(specification.getId(),adgs.getGoods().getId(),specification.getSpecification(),specification.getValue(),specification.getPicUrl(),null,currentTime,false);
            code2 =goodsSpecificationMapper.updateByPrimaryKey(goodsSpecification);
           // code2 =goodsSpecificationMapper.insert(goodsSpecification);
        }


        int code3 =0;
        for (AdminGoodsUpdateShoppingInformationOfGoodsListVO.ProductsBean product : products) {
            List<String> list = product.getSpecifications();  /*这个Specifications存一个list*/
            String[] strings = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                strings[i] = list.get(i);
            }
            GoodsProduct goodsProduct = new GoodsProduct(product.getId(),adgs.getGoods().getId(),strings,product.getPrice(),product.getNumber(),product.getUrl(),null,currentTime,false);
            code3 = goodsProductMapper.updateByPrimaryKey(goodsProduct);
        }

        int code4 =0;
        for (AdminGoodsUpdateShoppingInformationOfGoodsListVO.AttributesBean attribute : attributes) {
            GoodsAttribute goodsAttribute = new GoodsAttribute(attribute.getId(),adgs.getGoods().getId(),attribute.getAttribute(),attribute.getValue(),null,currentTime,false);
            if (attribute.getId()!=0){
                code4 =  goodsAttributeMapper.updateByStringValue(goodsAttribute);
            }
            code4 =1;
        }

        if (insert == 1 && code2 ==1 && code3 ==1 && code4 == 1){
            /*失败原因code3等于0*/
            return 200;
        }
        return 404;
    }

    @Override
    public Long countSearchGoodsAll() {
        GoodsExample goodsExample = new GoodsExample();
        // 构造查询条件
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        Long code = goodsMapper.countByExample(goodsExample);
        return code;
    }

    /*将字符串转为Integr数组*/
    public static String[] stringToInteger(String str){
        String[] string = null;
        if ("".equals(str)){
            return null;
        }
        java.lang.System.out.println(str);
        /*首先将字符串分割*/
        String str2 = str.replace("[","").replace("]","");
        string = str2.split(",");
        java.lang.System.out.println(string.length);

        return string;
    }
}
